﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class GXPackageEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["Id"] != null)
                {
                    int id = int.Parse(Request.Params["Id"]);
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    CMS_Product model = ctx.CMS_Product.Single(q => q.Id == id);
                    Name.Value = model.Name;
                    Remarks.Value = model.Remarks;
                    OrderNo.Value = model.OrderNo.ToString();
                    IsValid.Value = model.IsValid.ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            CMS_Product model;
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                model = ctx.CMS_Product.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                model = new CMS_Product();
                ctx.CMS_Product.InsertOnSubmit(model);
            }
            model.Name = Name.Value;
            model.Remarks = Remarks.Value;
            model.OrderNo = int.Parse(OrderNo.Value);
            model.IsValid =IsValid.Value=="1";
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}